package com.chujian.Activities;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

import com.chujian.Adapter.TableListAdapter;
import com.chujian.Threads.HttpClientPostThread;
import com.chujian.Threads.HttpClientGetThread;
import com.chujian.Utils.Constan;
import com.chujian.Utils.JsonUtil;
import com.chujian.model.Table;
import com.chujian.orderingsystem.R;

public class TableListActivity extends Activity implements OnClickListener,
	OnItemClickListener,OnItemLongClickListener{
	private ListView mListView;
	private Button mBtnAdd;
	private ImageButton mIbBack;
	private List<Table> mList;
	private int deletePosition;
	private int modifyPosition;
	private TableListAdapter mAdapter;
	private Handler mHandler=new Handler(){
		@Override
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			String str=(String) msg.obj;
			if(!str.equals("")&&!(str==null)){
				if(str.equals("deleteSuccess")){
					Toast.makeText(TableListActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
					mList.remove(deletePosition);
					mAdapter.notifyDataSetChanged();
					
				}else if(str.equals("deleteFailed")){
					Toast.makeText(TableListActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
				}else{
					mList=JsonUtil.TableListFromJson(str);
					mAdapter=new TableListAdapter(mList, TableListActivity.this);
					mListView.setAdapter(mAdapter);
				}
			}
		};
	};
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.table_list);
		initView();
		String url=Constan.BASE_URL+"TableListServlet";
		new HttpClientGetThread(url, mHandler).start();;
		
	}
	private void initView() {
		// TODO Auto-generated method stub
		mListView=(ListView) findViewById(R.id.lv_table_list);
		mBtnAdd=(Button) findViewById(R.id.btn_table_list_add);
		mIbBack=(ImageButton) findViewById(R.id.ib_header_back);
		mListView.setOnItemClickListener(this);
		mListView.setOnItemLongClickListener(this);
		mBtnAdd.setOnClickListener(this);
		mIbBack.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.ib_header_back:
			TableListActivity.this.finish();
			break;
		case R.id.btn_table_list_add:
//			添加餐桌的操作
			Intent intent=new Intent(TableListActivity.this, TableAddActivity.class);
			startActivityForResult(intent, 1000); 
		default:
			break;
		}
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		// TODO Auto-generated method stub
		deleteOrModifyDialog(position);
		return true;
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode==0)return ;
		Table table=(Table) data.getSerializableExtra("table1");
		if(1001==requestCode&&resultCode==1001){
//			修改餐桌
			mList.get(modifyPosition).setTableCapacity(table.getTableCapacity());
			mList.get(modifyPosition).setTableName(table.getTableName());
			mAdapter.notifyDataSetChanged();
		}else if(1000==requestCode&&1000==resultCode){
//			添加新的餐桌
			mList.add(table);
			mAdapter.notifyDataSetChanged();
		}
		
		
	}
	private void deleteOrModifyDialog(final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(TableListActivity.this);
        builder.setTitle(mList.get(position).getTableName());
        builder.setMessage("请选择操作");
        builder.setPositiveButton("删除", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which){
            	confirmDialog(position);
            }
        });
        builder.setNegativeButton("修改", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which){
            Table table=mList.get(position);
            modifyPosition=position;
            Intent intent=new Intent(TableListActivity.this, TableAddActivity.class);  
            Bundle bundle=new Bundle();  
            bundle.putSerializable("table", table);//序列化  
            intent.putExtras(bundle);//发送数据  
            startActivityForResult(intent, 1001);//启动intent  

            }
        });
        builder.show();
	}
	private void confirmDialog(final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(TableListActivity.this);
        builder.setTitle(mList.get(position).getTableName());
		builder.setMessage("确认删除吗？");
		builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// 记住当前item位置
				deletePosition = position;
				String url = Constan.BASE_URL + "TableDeleteServlet";
				new HttpClientPostThread(url, mHandler, mList.get(position)
						.getTableName()).start();
				;
			}
		});
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which){
            	
            }
        });
        builder.show();
	}
}
